﻿using MVC_No08.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_No08.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(string Name = null, string Context = null, string Type = null)
        {
            #region 查询
            var defectList = Function.GetAllDefect();
            using (Model1 model = new Model1())
            {
                if (!string.IsNullOrEmpty(Name) && int.TryParse(Name, out int i))
                {
                    int nameId = Convert.ToInt32(Name);
                    if(nameId != 0)
                        defectList = defectList.Where(u => u.DeviceID == nameId).ToList();
                }
                if (!string.IsNullOrEmpty(Context))
                    defectList = defectList.Where(u => u.Content.IndexOf(Context) >= 0).ToList();
                if (!string.IsNullOrEmpty(Type) && int.TryParse(Type, out int j))
                {
                    int type = Convert.ToInt32(Type);
                    if(type != 0)
                        defectList = defectList.Where(u => u.DefectState == type).ToList();
                }
            }


            #endregion



            var deviceList = Function.GetAllDevice();

            Dictionary<int, string> deviceDictionary = new Dictionary<int, string>();
            foreach (var item in deviceList)
                deviceDictionary.Add(item.DeviceID, item.DeviceName);
            ViewBag.deviceList = deviceDictionary;



            #region 载入查询

            #region 所属设备
            List<SelectListItem> DropDownListName = new List<SelectListItem>();
            DropDownListName.Add(new SelectListItem() { Value = "0", Text = "全部" });
            foreach (var item in Function.GetAllDevice())
                DropDownListName.Add(new SelectListItem() { Value = item.DeviceID.ToString(), Text = item.DeviceName });
            ViewBag.DropDownListName = DropDownListName;
            #endregion

            #endregion

            return View(defectList);
        }

        [HttpPost]
        public ActionResult Update(string ID)
        {
            if (!string.IsNullOrEmpty(ID) && int.TryParse(ID, out int i))
            {
                var id = Convert.ToInt32(ID);
                using (Model1 modle = new Model1())
                {
                    var info = modle.Defect.FirstOrDefault(u => u.DefectID == id);
                    info.DefectState = 2;
                    info.DealTime = DateTime.Now;

                    return Json(modle.SaveChanges());
                }
            }
            return Json(3);
        }

        [HttpPost]
        public ActionResult Delect(string ID)
        {
            if (!string.IsNullOrEmpty(ID) && int.TryParse(ID, out int i))
            {
                var id = Convert.ToInt32(ID);
                using (Model1 model = new Model1())
                {
                    var info = model.Defect.FirstOrDefault(u => u.DefectID == id);
                    if (info != null)
                        model.Defect.Remove(info);
                    return Json(model.SaveChanges());
                }
            }
            return Json(3);
        }

        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.deviceList = new SelectList(Function.GetAllDevice(), "DeviceID", "DeviceName");

            ViewBag.defectTypeList = new SelectList(new List<SelectListItem>() {
                new SelectListItem(){ Value = "高",Text = "1" },
                new SelectListItem(){ Value = "中",Text = "2" },
                new SelectListItem(){ Value = "低",Text = "3" },
            }, "Text", "Value");

            return View(new Defect());
        }

        [HttpPost]
        public ActionResult Create(Defect defect)
        {
            if (ModelState.IsValid)
            {
                defect.AddTime = DateTime.Now;
                defect.DefectState = 1;

                using (Model1 model = new Model1())
                {
                    model.Defect.Add(defect);
                    if (model.SaveChanges() == 1)
                        return RedirectToAction("Index", "MyHome");
                }
            }
            return RedirectToAction("Create", "MyHome");
        }

    }


    static class Function
    {
        public static List<Defect> GetAllDefect()//获取Defect全部内容
        {
            using (Model1 model = new Model1())
            {
                var allDefectList = model.Defect.Where(u => true).ToList();

                return allDefectList;
            }
        }

        public static List<Device> GetAllDevice()//获取Device全部内容
        {
            using (Model1 model = new Model1())
            {
                var allDeviceList = model.Device.Where(u => true).ToList();

                return allDeviceList;
            }
        }


    }
}